import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {HttpService} from '../../lib/service/http.service';
import {adSystemApiUrl, appletApiUrl} from '../../../config/api';
import {LocalStorageService} from '../../lib/service/local-storage.service';

@Injectable()
export class OrderService {

  constructor(private httpService: HttpService, private localStorageService: LocalStorageService) {
  }

  // 类目
  getOrderList(
    shop_id,
    order_no = '', // 订单编号
    nick_name = '', // 卖家昵称
    goods_name = '', // 商品名称
    phone = '',  // 手机号
    order_type = 0, // 订单类型 0  全部 1普通 2拼团
    type = '0', // 订单状态 0全部 1待付款 2待发货 3待收货 4交易完成 5售后
    start_time = '',
    end_time = '',
    page,
    pageSize = 10,
    order_by) {

    let startTime = 0;
    let endTime = 0;

    if (!!start_time) {
      startTime = new Date(Date.parse(start_time.replace(/-/g, '/'))).getTime() / 1000;
    }
    if (!!end_time) {
      endTime = new Date(Date.parse(end_time.replace(/-/g, '/'))).getTime() / 1000;
    }
    let url = `${appletApiUrl}/backend/order/index`;
    // if (order_by.length > 2) {
    //   url += `&order_by=${order_by}`;
    // }
    return this.httpService.httpPostAuth(url, {page, pageSize, shop_id: shop_id}).map(rs => rs);
  }

  getRefundReason() {
    const url = `${adSystemApiUrl}/v1/applet/order/get_refund_reason`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getRefundOrder(order_sub_id) {
    const url = `${adSystemApiUrl}/v1/member/order/get_refund_order?order_sub_id=${order_sub_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  agreeRefund(order_sub_id, refund_remark) {
    const url = `${adSystemApiUrl}/v1/member/order/confirm_refund`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      order_sub_id: order_sub_id,
      remark: refund_remark
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  refuseRefund(order_sub_id, refund_remark) {
    const url = `${adSystemApiUrl}/v1/member/order/refuse_refund`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      order_sub_id: order_sub_id,
      remark: refund_remark
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 导出选中
  down_xsl(data) {
    const url = `${adSystemApiUrl}/v1/member/order/down_xsl`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers, responseType: 3});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 获取快递公司列表
  getExpressList() {
    const url = `${adSystemApiUrl}/v1/member/express/list?page=1&pageSize=999`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 发货详情
  getExpressInfo(order_no) {
    const url = `${adSystemApiUrl}/v3/member/order/express_info?order_no=${order_no}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 发货
  editExpress(body) {
    const url = `${adSystemApiUrl}/v3/member/order/edit_express`;
    return this.httpService.httpPostAuth(url, body).map(rs => rs);
  }

  // 修改发货地址
  editExpressAddress(body) {
    const url = `${adSystemApiUrl}/v3/member/order/edit_express_address`;
    return this.httpService.httpPostAuth(url, body).map(rs => rs);
  }

  // 获取物流信息
  getOrderLogistics(order_no) {
    const url = `${adSystemApiUrl}/v1/common/express_by_order_no?order_no=${order_no}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
}
